package com.hxzy.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.dto.SubCartDTO;
import com.hxzy.entity.ShoppingCart;
import com.hxzy.service.ShoppingCartService;
import com.hxzy.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "购物车API")
@RestController
@RequestMapping(value = "/app/cart")
public class ShoppingCartController  extends BaseController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation(value = "用户某个商家的购物车")
    @GetMapping(value = "/merchantId/{id}")
    public R getUserCart(@PathVariable(value = "id") Long merchantId){
        List<ShoppingCart> arr=this.shoppingCartService.findUserCartByMerchandId(merchantId);

        return  R.okHasData(arr);
    }

    @ApiOperation(value = "添加到购物车")
    @PostMapping(value = "/add")
    public R addCart(@RequestBody @Valid ShoppingCart shoppingCart){
        /* {amount: 26, dishId: 1, merchantId: 1, name: "宫保鸡丁", image: null}
         */
        /* {amount: 25,  dishId: 4, merchantId: 1, name: "龙聚德烤鸭套饭",image: null，dishFlavor: "中辣,半糖"} */

        int number=this.shoppingCartService.addCart(shoppingCart);

        //返回
        Map<String ,Integer> data=new HashMap<>();
        data.put("number",number);
        return R.okHasData(data);
    }

    @ApiOperation(value = "减少购物车的商品数量")
    @PostMapping(value = "/sub/{dishId}")
    public R subCart( @PathVariable(value = "dishId")  Long dishId ){
        if(dishId==null){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

         int number=this.shoppingCartService.subCart(dishId);
        //返回
        Map<String ,Integer> data=new HashMap<>();
        data.put("number",number);
        return R.okHasData(data);
    }

    @ApiOperation(value = "清除当前商家的购物车")
    @DeleteMapping(value = "/clean/{merchantId}")
    public R clean(@PathVariable(value = "merchantId") Long merchantId){
        if(merchantId==null){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }
        this.shoppingCartService.clean(merchantId);
        return R.ok();
    }
}
